import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {getPushRecordStatistics} from "@/api/message/pushRecord";

const message = {
    state:{
        unreadNumber: storage.get(constant.unreadNumber) || 0,
    },
    mutations:{
        SET_UNREAD_NUMBER(state, unreadNumber){
            state.unreadNumber = unreadNumber
            storage.set(constant.unreadNumber, unreadNumber)
        }
    },
    actions:{
        setUnreadNumber({commit}, unreadNumber){
            commit('SET_UNREAD_NUMBER', unreadNumber)
        },
        // 获取未读消息
        getUnreadNumber({commit}){
            return new Promise((resolve, reject) => {
                getPushRecordStatistics().then(res => {
                    if(res.code === 200){
                        commit('SET_UNREAD_NUMBER', res.data)
                        resolve(res.data)
                    }
                }).catch(err => {
                    reject(err)
                })
            })
        }
    }
}
export default message